package org.damazio.bhtranscams;

import java.util.List;

import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

/**
 * Activity which shows the map with all the cameras in it. This is never
 * started independently, but rather as the contents of a tab in the main
 * screen.
 * 
 * @author rdamazio
 */
public class CamerasMap extends MapActivity {
  private static final double BELO_LATITUDE = -19.927302;
  private static final double BELO_LONGITUDE = -43.938103;
  private static final int BELO_ZOOM_LEVEl = 15;
  private MyLocationOverlay myLocationOverlay;

  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.map);

    // Get the map and its elements
    MapView map = (MapView) findViewById(R.id.mapview);
    MapController mapController = map.getController();
    List<Overlay> overlays = map.getOverlays();

    // Set the current location
    GeoPoint beloPoint = createGeoPointFor(BELO_LATITUDE, BELO_LONGITUDE);
    mapController.setCenter(beloPoint);
    mapController.setZoom(BELO_ZOOM_LEVEl);
    map.setBuiltInZoomControls(true);

    // Add "my location" overlay
    myLocationOverlay = new MyLocationOverlay(this, map);
    overlays.add(myLocationOverlay);

    // Create markers for the cameras
    for (Cameras camera : Cameras.values()) {
      GeoPoint cameraPoint = createGeoPointFor(camera.getLatitude(), camera.getLongitude());
      CamerasMapOverlay overlay = new CamerasMapOverlay(this, cameraPoint);
      overlays.add(overlay);
    }

    // Make sure the overlays are added
    map.invalidate();
  }

  @Override
  protected void onPause() {
    myLocationOverlay.disableMyLocation();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    myLocationOverlay.enableMyLocation();
  }

  @Override
  protected boolean isRouteDisplayed() {
    return false;
  }

  /**
   * Creates and returns a {@link GeoPoint} class with the given latitude and
   * longitude.
   */
  private GeoPoint createGeoPointFor(double latitude, double longitude) {
    return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
  }
}
